<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: AlbumController.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_AlbumController extends Core_Controller_Action_Standard {
	
	public function init() {
		
		//HERE WE CHECKING THE SITEPAGE ALBUM IS ENABLED OR NOT
		$sitepagealbumEnabled = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' );
		if (! $sitepagealbumEnabled) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		$ajaxContext = $this->_helper->getHelper ( 'AjaxContext' );
		$ajaxContext->addActionContext ( 'rate', 'json' )->addActionContext ( 'validation', 'html' )->initContext ();
		$page_id = $this->_getParam ( 'page_id', $this->_getParam ( 'id', null ) );
		
		//PACKAGE BASE PRIYACY START
		if (! empty ( $page_id )) {
			$sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $page_id );
			if ($sitepage) {
				Engine_Api::_ ()->core ()->setSubject ( $sitepage );
			}
			if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
				if (! Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagealbum" )) {
					return $this->_forward ( 'requireauth', 'error', 'core' );
				}
			} else {
				$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'spcreate' );
				if (empty ( $isPageOwnerAllow )) {
					return $this->_forward ( 'requireauth', 'error', 'core' );
				}
			}
		} //PACKAGE BASE PRIYACY END
		else {
			if (Engine_Api::_ ()->core ()->hasSubject () != null) {
				$photo = Engine_Api::_ ()->core ()->getSubject ();
				$album = $photo->getCollection ();
				$page_id = $album->page_id;
			}
		}
	}
	
	//ACTION FOR EDIT THE ALBUM
	public function editAction() {
		
		//CHECK USER VALIDATION
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		//CHECK REQUERIED SUBJECT IS THERE OR NOT
		if (! $this->_helper->requireSubject ( 'sitepage_page' )->isValid ())
			return;
		
		//GET ALBUM ID
		$album_id = $this->_getParam ( 'album_id' );
		
		//GET PAGE ID
		$page_id = $this->_getParam ( 'page_id' );
		
		//GET SITEPAGE ITEM
		$sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $page_id );
		
		//START MANAGE-ADMIN CHECK
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'edit' );
		if (empty ( $isManageAdmin )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		//END MANAGE-ADMIN CHECK
		//GET ALBUM ITEM
		$album = Engine_Api::_ ()->getItem ( 'sitepage_album', $album_id );
		if (! Engine_Api::_ ()->core ()->hasSubject ( 'sitepage_page' )) {
			Engine_Api::_ ()->core ()->setSubject ( $album );
		}
		
		//MAKE FORM
		$this->view->form = $form = new Sitepage_Form_Album_Edit ();
		
		//START PHOTO PRIVACY WORK
		$sitepagealbumEnabled = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagealbum' );
		if ($sitepagealbumEnabled) {
			$auth = Engine_Api::_ ()->authorization ()->context;
			$roles = array ('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered' );
			foreach ( $roles as $role ) {
				if ($form->auth_tag) {
					if (1 == $auth->isAllowed ( $album, $role, 'tag' )) {
						$form->auth_tag->setValue ( $role );
					}
				}
			}
		}
		//END PHOTO PRIVACY WORK
		//COMMENT PRIVACY
		$auth = Engine_Api::_ ()->authorization ()->context;
		$roles = array ('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone' );
		$commentMax = array_search ( "everyone", $roles );
		foreach ( $roles as $i => $role ) {
			$auth->setAllowed ( $album, $role, 'comment', ($i <= $commentMax) );
		}
		//END PHOTO PRIVACY	WORK
		//CHECK FORM VALIDATION
		if (! $this->getRequest ()->isPost ()) {
			$form->populate ( $album->toArray () );
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Invalid request method' );
			return;
		}
		
		//CHECK FORM VALIDATION
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Invalid data' );
			return;
		}
		
		//PROCESS
		$db = $album->getTable ()->getAdapter ();
		$db->beginTransaction ();
		try {
			//GET FORM VALUES
			$values = $form->getValues ();
			$album->setFromArray ( $values );
			$album->save ();
			
			//CREATE AUTH STUFF HERE
			$auth = Engine_Api::_ ()->authorization ()->context;
			$roles = array ('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered' );
			
			//REBUILD PRIVACY
			$actionTable = Engine_Api::_ ()->getDbtable ( 'actions', 'activity' );
			foreach ( $actionTable->getActionsByObject ( $album ) as $action ) {
				$actionTable->resetActivityBindings ( $action );
			}
			
			//START TAG PRIVACY
			if (empty ( $values ['auth_tag'] )) {
				$values ['auth_tag'] = key ( $form->auth_tag->options );
				if (empty ( $values ['auth_tag'] )) {
					$values ['auth_tag'] = 'registered';
				}
			}
			$tagMax = array_search ( $values ['auth_tag'], $roles );
			foreach ( $roles as $i => $role ) {
				$auth->setAllowed ( $album, $role, 'tag', ($i <= $tagMax) );
			}
			//END TAG PRIVACY
			//COMMIT
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		return $this->_forward ( 'success', 'utility', 'core', array ('messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Changes saved.' ) ), 'smoothboxClose' => 300, 'parentRefresh' => 300 ) );
	}
	
	//ACTION FOR VIEW THE ALBUM
	public function viewAction() {
		
		//GET ALBUM ID
		$this->view->album_id = $album_id = $this->_getParam ( 'album_id' );
		
		//GET VIEWER ID
		$viewer_id = Engine_Api::_ ()->user ()->getViewer ()->getIdentity ();
		
		//GET ALBUM ITEM
		$this->view->album = $album = Engine_Api::_ ()->getItem ( 'sitepage_album', $album_id );
		
		//SEND TAB ID TO THE TPL
		$this->view->tab_selected_id = $this->_getParam ( 'tab' );
		
		$getPackageEditPhoto = Engine_Api::_ ()->sitepage ()->getPackageAuthInfo ( 'sitepagealbum' );
		
		//GET PAGE ID
		$page_id = $this->_getParam ( 'page_id' );
		
		//GET SITEPAGE ITEM
		$this->view->sitepage = $sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $page_id );
		
		//START MANAGE-ADMIN CHECK
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'view' );
		if (empty ( $isManageAdmin )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'comment' );
		if (empty ( $isManageAdmin )) {
			$this->view->can_comment = 0;
		} else {
			$this->view->can_comment = 1;
		}
		
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'spcreate' );
		if (empty ( $isManageAdmin )) {
			$this->view->canCreatePhoto = $canCreatePhoto = 0;
		} else {
			$this->view->canCreatePhoto = $canCreatePhoto = 1;
		}
		
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'edit' );
		if (empty ( $isManageAdmin )) {
			$this->view->can_edit = 0;
		} else {
			$this->view->can_edit = 1;
		}
		//END MANAGE-ADMIN CHECK
		//CHECK THAT USER CAN UPLOAD PHOTO OR NOT
		$this->view->upload_photo = 0;
		
		if ($canCreatePhoto == 1 && (Engine_Api::_ ()->sitepage ()->isPageOwner ( $sitepage ) || $album->default_value == 1)) {
			$this->view->upload_photo = 1;
		}
		
		//GET CURRENT PAGE NUMBER
		$currentPageNumbers = $this->_getParam ( 'pages', 1 );
		
		//SEND CURRENT PAGE NUMBER TO THE TPL
		$this->view->currentPageNumbers = $currentPageNumbers;
		
		//SEND PHOTOS PER PAGE TO THE TPL
		$this->view->photos_per_page = $photos_per_page = 20;
		
		//SET PAGE PHOTO PARAMS
		$paramsPhoto = array ();
		$paramsPhoto ['page_id'] = $page_id;
		$paramsPhoto ['album_id'] = $album_id;
		$paramsPhoto ['order'] = 'order ASC';
		$paramsPhoto ['getSpecialField'] = 1;
		
		//FETCHING ALL PHOTOS
		$fecthPhotos = Engine_Api::_ ()->getDbtable ( 'photos', 'sitepage' )->getPhotos ( $paramsPhoto )->toarray ();
		$total_photo = 0;
		if (! empty ( $fecthPhotos )) {
			$total_photo = $fecthPhotos [0] ['count'];
			if (Engine_Api::_ ()->core ()->hasSubject ()) {
				Engine_Api::_ ()->core ()->clearSubject ();
			}
			Engine_Api::_ ()->core ()->setSubject ( $album );
		}
		
		//SET DEFAULT ALBUM VALUE
		$this->view->default_value = $album->default_value;
		
		//SET ALBUMS PARAMS
		$paramsAlbum = array ();
		$paramsAlbum ['page_id'] = $page_id;
		$paramsAlbum ['getSpecialField'] = 1;
		
		//FETCH ALBUMS
		$fecthAlbums = Engine_Api::_ ()->getDbtable ( 'albums', 'sitepage' )->getAlbums ( $paramsAlbum )->toarray ();
		if (! empty ( $fecthAlbums )) {
			$this->view->album_count = $fecthAlbums [0] ['count'];
		}
		
		//MAKING PAGINATION 
		$page_vars = Engine_Api::_ ()->sitepage ()->makePage ( $total_photo, $photos_per_page, $currentPageNumbers );
		$page_array = array ();
		for($x = 0; $x <= $page_vars [2] - 1; $x ++) {
			if ($x + 1 == $page_vars [1]) {
				$link = "1";
			} else {
				$link = "0";
			}
			$page_array [$x] = array ('page' => $x + 1, 'link' => $link );
		}
		$this->view->pagearray = $page_array;
		$this->view->maxpage = $page_vars [2];
		$this->view->pstart = 1;
		
		//GET TOTAL IMAGES
		$this->view->total_images = empty ( $getPackageEditPhoto ) ? null : $total_photo;
		
		//SET PAGE PHOTO PARAMS
		$paramsPhoto ['start'] = $photos_per_page;
		$paramsPhoto ['end'] = $page_vars [0];
		$paramsPhoto ['getSpecialField'] = 0;
		
		//GETTING THE PHOTOS ACCORDING TO LIMIT
		$this->view->photos = Engine_Api::_ ()->getDbtable ( 'photos', 'sitepage' )->getPhotos ( $paramsPhoto );
		
		//INCREMENT VIEWS
		if (! $album->getOwner ()->isSelf ( Engine_Api::_ ()->user ()->getViewer () )) {
			$album->view_count ++;
		}
		
		//SAVE
		$album->save ();
		
		//CHECK PHOTO SHOULD BE SHOW IN THE LIGHTBOX OR NOT
		$this->view->showLightBox = Engine_Api::_ ()->sitepage ()->canShowPhotoLightBox ();
		
		//START: "SUGGEST TO FRIENDS" LINK WORK
		$page_flag = 0;
		$is_suggestion_enabled = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'suggestion' );
		if (! empty ( $is_suggestion_enabled )) {
			$is_moduleEnabled = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepage' );
			$isSupport = Engine_Api::_ ()->getApi ( 'suggestion', 'sitepage' )->isSupport ();
			//HERE WE ARE DELETE THIS ALBUM SUGGESTION IF VIEWER HAVE
			if (! empty ( $is_moduleEnabled )) {
				Engine_Api::_ ()->getApi ( 'suggestion', 'sitepage' )->deleteSuggestion ( $viewer_id, 'page_album', $this->_getParam ( 'album_id' ), 'page_album_suggestion' );
			}
			
			$SuggVersion = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->getModule ( 'suggestion' )->version;
			$versionStatus = strcasecmp ( $SuggVersion, '4.1.7p1' );
			if ($versionStatus >= 0) {
				$modContentObj = Engine_Api::_ ()->suggestion ()->getSuggestedFriend ( 'sitepagealbum', $this->_getParam ( 'album_id' ), 1 );
				if (! empty ( $modContentObj )) {
					$contentCreatePopup = @COUNT ( $modContentObj );
				}
			}
			
			if (Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.package.enable', 1 )) {
				if ($sitepage->expiration_date <= date ( "Y-m-d H:i:s" )) {
					$page_flag = 1;
				}
			}
			if (! empty ( $contentCreatePopup ) && ! empty ( $isSupport ) && empty ( $sitepage->closed ) && ! empty ( $sitepage->approved ) && empty ( $sitepage->declined ) && ! empty ( $sitepage->draft ) && empty ( $page_flag ) && ! empty ( $viewer_id ) && ! empty ( $is_suggestion_enabled )) {
				$this->view->albumSuggLink = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.albumsugg.link', 0 );
			}
		}
	
		//END: "SUGGEST TO FRIENDS" LINK WORK    
	}
	
	//ACTION FOR DELETE THE ALBUM
	public function deleteAction() {
		
		//CHECK USER VALIDATION
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		//CHECK REQUERIED SUBJECT IS THERE OR NOT
		if (! $this->_helper->requireSubject ( 'sitepage_page' )->isValid ())
			return;
		
		//GET PAGE ID
		$page_id = $this->_getParam ( 'page_id', null );
		
		//GET SITEPAGE ITEM
		$sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $page_id );
		
		//START MANAGE-ADMIN CHECK
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'edit' );
		if (empty ( $isManageAdmin )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		//END MANAGE-ADMIN CHECK
		//GET ALBUM ID
		$album_id = $this->_getParam ( 'album_id', $this->_getParam ( 'album_id', null ) );
		
		//GET ALBUM ITEM
		$album = Engine_Api::_ ()->getItem ( 'sitepage_album', $album_id );
		
		//GET DELETE FORM
		$this->view->form = $form = new Sitepage_Form_Album_Delete ();
		
		//CHECK ALBUM EXIST OR NOT TO DELETE
		if (! $album) {
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( "Album doesn't exist or not authorized to delete." );
			return;
		}
		
		//CHECK FORM VALIDATION
		if (! $this->getRequest ()->isPost ()) {
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Invalid request method' );
			return;
		}
		
		//GET DB
		$db = $album->getTable ()->getAdapter ();
		$db->beginTransaction ();
		try {
			
			//DELETE ALBUM
			$album->delete ();
			
			//COMMIT
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 2, 'parentRedirect' => $this->_helper->url->url ( array ('page_url' => Engine_Api::_ ()->sitepage ()->getPageUrl ( $page_id ), 'tab' => $this->_getParam ( 'tab' ) ), 'sitepage_entry_view' ), 'parentRedirectTime' => '2', 'format' => 'smoothbox', 'messages' => Zend_Registry::get ( 'Zend_Translate' )->_ ( "Album has been deleted." ) ) );
	}
	
	//ACTION FOR EDIT PHOTOS TO THE ALBUM
	public function editPhotosAction() {
		
		//CHECK USER VALIDATION
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		//CHECK REQUERIED SUBJECT IS THERE OR NOT
		if (! $this->_helper->requireSubject ( 'sitepage_page' )->isValid ())
			return;
		
		//GET SITEPAGE ITEM
		$this->view->sitepage = $sitepage = Engine_Api::_ ()->core ()->getSubject ( 'sitepage_page' );
		
		//SEND TAB ID TO THE TPL
		$this->view->tab_selected_id = $this->_getParam ( 'tab' );
		
		//START MANAGE-ADMIN CHECK
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'edit' );
		if (empty ( $isManageAdmin )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		//END MANAGE-ADMIN CHECK
		//GET ALBUM ID
		$album_id = $this->view->album_id = $this->_getParam ( 'album_id' );
		
		//GET PAGE ID
		$page_id = $this->view->page_id = $this->_getParam ( 'page_id' );
		
		//GET REQUEST ISAJAX OR NOT
		$isajax = $this->_getParam ( 'is_ajax' );
		
		//GET ITEM ALBUM
		$this->view->album = $album = $sitepage->getSingletonAlbum ( $album_id );
		
		//GET NAVIGATION
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_main' );
		
		//SEND CURRENT PAGE NUMBER TO THE TPL
		$this->view->currentPageNumbers = $currentPageNumbers = $this->_getParam ( 'pages', 1 );
		
		//SEND PHOTOS PER PAGE TO THE TPL
		$this->view->photos_per_page = $photos_per_page = 20;
		
		//SET PAGE PHOTO PARAMS
		$paramsPhoto = array ();
		$paramsPhoto ['page_id'] = $page_id;
		$paramsPhoto ['album_id'] = $album_id;
		$paramsPhoto ['order'] = 'order ASC';
		$paramsPhoto ['getSpecialField'] = 1;
		
		//FETCHING ALL PHOTOS
		$fecthPhotos = Engine_Api::_ ()->getDbtable ( 'photos', 'sitepage' )->getPhotos ( $paramsPhoto )->toarray ();
		$total_photo = 0;
		if (! empty ( $fecthPhotos )) {
			$total_photo = $fecthPhotos [0] ['count'];
		}
		
		$page_vars = Engine_Api::_ ()->sitepage ()->makePage ( $total_photo, $photos_per_page, $currentPageNumbers );
		$page_array = Array ();
		for($x = 0; $x <= $page_vars [2] - 1; $x ++) {
			if ($x + 1 == $page_vars [1]) {
				$link = "1";
			} else {
				$link = "0";
			}
			$page_array [$x] = Array ('page' => $x + 1, 'link' => $link );
		}
		$this->view->pagearray = $page_array;
		$this->view->maxpage = $page_vars [2];
		$this->view->pstart = 1;
		$this->view->total_images = $total_photo;
		
		//SET LIMIT PARAMS
		$paramsPhoto ['start'] = $photos_per_page;
		$paramsPhoto ['end'] = $page_vars [0];
		$paramsPhoto ['getSpecialField'] = 0;
		
		//GETTING THE PHOTOS ACCORDING TO LIMIT
		$this->view->photos = Engine_Api::_ ()->getDbtable ( 'photos', 'sitepage' )->getPhotos ( $paramsPhoto );
		
		//MAKE EDIT PHOTOS FORM
		$this->view->form = $form = new Sitepage_Form_Album_Photos ();
		foreach ( $this->view->photos as $photo ) {
			$subform = new Sitepage_Form_Photo_Edit ( array ('elementsBelongTo' => $photo->getGuid () ) );
			$subform->populate ( $photo->toArray () );
			$form->addSubForm ( $subform, $photo->getGuid () );
			$form->cover->addMultiOption ( $photo->file_id, $photo->file_id );
		}
		
		//CHECK FORM VALIDATION
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		//CHECK FORM VALIDATION
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			//return;
		}
		
		//GET DB
		$db = $album->getTable ()->getAdapter ();
		$db->beginTransaction ();
		;
		try {
			//GET FORM VALUES
			$values = $form->getValues ();
			if (! empty ( $values ['cover'] )) {
				$album->photo_id = $values ['cover'];
				$album->save ();
			}
			//PROCESS
			foreach ( $this->view->photos as $photo ) {
				$subform = $form->getSubForm ( $photo->getGuid () );
				$values = $subform->getValues ();
				$values = $values [$photo->getGuid ()];
				
				//UNSET TEH PHOTO ID
				unset ( $values ['photo_id'] );
				
				if (isset ( $values ['delete'] ) && $values ['delete'] == '1') {
					$photo->delete ();
					$paramsPhoto ['getSpecialField'] = 1;
					
					//FETCHING ALL PHOTOS
					$fecthPhotos = Engine_Api::_ ()->getDbtable ( 'photos', 'sitepage' )->getPhotos ( $paramsPhoto )->toarray ();
					if (empty ( $fecthPhotos )) {
						Engine_Api::_ ()->getItemTable ( 'sitepage_album' )->update ( array ('photo_id' => 0 ), array ('page_id =?' => $page_id, 'album_id =?' => $album_id ) );
					}
				} else {
					$photo->setFromArray ( $values );
					$photo->save ();
				}
			}
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		if (! $isajax) {
			return $this->_helper->redirector->gotoRoute ( array ('action' => 'view', 'page_id' => $page_id, 'album_id' => $album_id, 'slug' => $album->getSlug (), 'tab' => $this->view->tab_selected_id ), 'sitepage_albumphoto_general', true );
		}
	}
	
	public function viewAlbumAction() {
		
		//GET PAGE ID
		$page_id = $this->_getParam ( 'page_id', null );
		
		//SET SITEPAGE ITEM
		$this->view->sitepage = $sitepage = Engine_Api::_ ()->core ()->getSubject ( 'sitepage_page' );
		
		//SEND TAB ID TO THE TPL
		$this->view->tab_selected_id = $this->_getParam ( 'tab' );
		
		//START MANAGE-ADMIN CHECK
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'view' );
		if (empty ( $isManageAdmin )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		//END MANAGE-ADMIN CHECK
		//SET ALBUMS PARAMS
		$paramsAlbum = array ();
		$paramsAlbum ['page_id'] = $page_id;
		
		//FETCH ALBUMS
		$this->view->album = Engine_Api::_ ()->getDbtable ( 'albums', 'sitepage' )->getAlbums ( $paramsAlbum );
	}
	
	//ACTION FOR CHANGE THE ORDER OF THE PHOTOS
	public function orderAction() {
		
		//CHECK USER VALIDATION
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		//GET ALBUM ID
		$album_id = $this->_getParam ( 'album_id' );
		
		//GET PAGE ID
		$page_id = $this->_getParam ( 'page_id' );
		
		//GET ORDER
		$order = $this->_getParam ( 'order' );
		if (! $order) {
			$this->view->status = false;
			return;
		}
		
		//GET CURRENT ORDER
		$currentOrder = Engine_Api::_ ()->getDbtable ( 'photos', 'sitepage' )->getPagePhotosOrder ( $album_id, $page_id );
		
		//FIND THE STARTING POINT?
		$start = null;
		$end = null;
		for($i = 0, $l = count ( $currentOrder ); $i < $l; $i ++) {
			if (in_array ( $currentOrder [$i], $order )) {
				$start = $i;
				$end = $i + count ( $order );
				break;
			}
		}
		
		if (null === $start || null === $end) {
			$this->view->status = false;
			return;
		}
		
		for($i = 0, $l = count ( $currentOrder ); $i < $l; $i ++) {
			if ($i >= $start && $i <= $end) {
				$photo_id = $order [$i - $start];
			} else {
				$photo_id = $currentOrder [$i];
			}
			Engine_Api::_ ()->getItemTable ( 'sitepage_photo' )->update ( array ('order' => $i ), array ('photo_id = ?' => $photo_id ) );
		}
		$this->view->status = true;
	}
	
	//ACTION FOR CHANGE THE ORDER OF THE ALBUMS
	public function albumOrderAction() {
		
		//CHECK USER VALIDATION
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		//GET PAGE ID
		$page_id = $this->_getParam ( 'page_id' );
		
		//GET ORDER
		$order = $this->_getParam ( 'order' );
		if (! $order) {
			$this->view->status = false;
			return;
		}
		
		//GET CURRENT ORDER OF ALBUM
		$currentOrder = Engine_Api::_ ()->getDbtable ( 'albums', 'sitepage' )->getPageAlbumsOrder ( $page_id );
		
		//FIND THE STARTING POINT?
		$start = null;
		$end = null;
		for($i = 0, $l = count ( $currentOrder ); $i < $l; $i ++) {
			if (in_array ( $currentOrder [$i], $order )) {
				$start = $i;
				$end = $i + count ( $order );
				break;
			}
		}
		
		if (null === $start || null === $end) {
			$this->view->status = false;
			return;
		}
		
		for($i = 0, $l = count ( $currentOrder ); $i < $l; $i ++) {
			if ($i >= $start && $i <= $end) {
				$photo_id = $order [$i - $start];
			} else {
				$photo_id = $currentOrder [$i];
			}
			Engine_Api::_ ()->getItemTable ( 'sitepage_album' )->update ( array ('order' => $i ), array ('photo_id = ?' => $photo_id ) );
		}
		$this->view->status = true;
	}
	
	public function composeUploadAction() {
		if (! Engine_Api::_ ()->user ()->getViewer ()->getIdentity ()) {
			$this->_redirect ( 'login' );
			return;
		}
		
		if (! $this->getRequest ()->isPost ()) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Invalid method' );
			return;
		}
		
		if (empty ( $_FILES ['Filedata'] )) {
			$this->view->status = false;
			$this->view->error = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Invalid data' );
			return;
		}
		
		// Get album
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$table = Engine_Api::_ ()->getDbtable ( 'albums', 'sitepage' );
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$type = $this->_getParam ( 'type', 'wall' );
			
			if (empty ( $type ))
				$type = 'wall';
			$page_id = $this->_getParam ( 'page_id', $this->_getParam ( 'id', null ) );
			
			//PACKAGE BASE PRIYACY START
			$sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $page_id );
			
			$album = $table->getSpecialAlbum ( $sitepage, $type );
			
			$photoTable = Engine_Api::_ ()->getDbtable ( 'photos', 'sitepage' );
			$photo = $photoTable->createRow ();
			$photo->setFromArray ( array ('page_id' => $page_id, 'user_id' => Engine_Api::_ ()->user ()->getViewer ()->getIdentity () ) );
			$photo->save ();
			$photo->setPhoto ( $_FILES ['Filedata'] );
			
			if ($type == 'message') {
				$photo->title = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Attached Image' );
			}
			
			$photo->album_id = $album->album_id;
			$photo->collection_id = $album->album_id;
			$photo->save ();
			
			if (! $album->photo_id) {
				$album->photo_id = $photo->file_id;
				$album->save ();
			}
			
			if ($type != 'message') {
				// Authorizations
				$auth = Engine_Api::_ ()->authorization ()->context;
				$auth->setAllowed ( $photo, 'everyone', 'view', true );
				$auth->setAllowed ( $photo, 'everyone', 'comment', true );
			}
			
			$db->commit ();
			
			$this->view->status = true;
			$this->view->photo_id = $photo->photo_id;
			$this->view->album_id = $album->album_id;
			$this->view->src = $photo->getPhotoUrl ();
			$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Photo saved successfully' );
		} catch ( Exception $e ) {
			$db->rollBack ();
			//throw $e;
			$this->view->status = false;
		}
	}

}
?>